package com.superdry.activity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.w3c.dom.ls.LSInput;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.http.AbHttpUtil;
import com.ab.http.AbRequestParams;
import com.ab.http.AbStringHttpResponseListener;
import com.alibaba.fastjson.JSONObject;
import com.drysurper.R;
import com.google.gson.JsonObject;
import com.superdry.adapter.ExpandableListViewAdapter;
import com.superdry.json.Clothes;
import com.superdry.main.MainActivity;
import com.superdry.utils.GlobalConst;
import com.superdry.utils.SPHelper;

public class ChooseClothTypeActivity extends AbActivity implements OnClickListener {

	private ExpandableListView expandableListView;
	private ExpandableListViewAdapter treeViewAdapter;

	private TextView total_cloth_count;
	private TextView total_money;
	private Button ensure_choice;
	private LinearLayout choose_cloth_goback;
	private AbHttpUtil mAbHttpUtil = null;
	private List<String> groups = new ArrayList<String>();

	private List<Clothes> cloths;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 获取Http工具类
		mAbHttpUtil = AbHttpUtil.getInstance(this);
		mAbHttpUtil.setTimeout(10000);
		setContentView(R.layout.aty_choose_clothtype);
		initCloths();
		initViews();
	}

	/**
	 * 初始化衣服的类型
	 */
	private void initCloths() {
		cloths = new ArrayList<Clothes>();
		Clothes c1 = new Clothes(9, "T恤,衬衫,背心,马甲,长裤,短裤,短裙,内衣,内裤,睡裙,睡裤,帽子,领带,围巾,床单,枕套");
		Clothes c2 = new Clothes(19, "西装,体闲外套,牛仔衣,毛衣,针织衫,真丝上衣,亚麻上衣,连衣裙,棉马甲,羽绒马甲背心,羊毛马甲背心,羊绒羊毛衫,毛领,披肩,被套");
		Clothes c3 = new Clothes(29, "风衣,大衣,冲锋衣,羽绒衣,棉衣,睡袍,礼服,毛毯,床上四件套");
		cloths.add(c1);
		cloths.add(c2);
		cloths.add(c3);
	}

	/**
	 * 初始化组件
	 */
	private void initViews() {

		total_cloth_count = (TextView) findViewById(R.id.total_cloth_count);
		total_money = (TextView) findViewById(R.id.total_money);
		ensure_choice = (Button) findViewById(R.id.ensure_choice);
		choose_cloth_goback = (LinearLayout) findViewById(R.id.choose_cloth_goback);

		ensure_choice.setOnClickListener(this);
		choose_cloth_goback.setOnClickListener(this);
		setListView(cloths);
		/*
		 * mAbHttpUtil.post(GlobalConst.GET_CLOTHES_TYPE, params, new
		 * AbStringHttpResponseListener() {
		 * 
		 * @Override public void onStart() { }
		 * 
		 * @Override public void onFinish() { }
		 * 
		 * @Override public void onFailure(int statusCode, String content,
		 * Throwable error) { }
		 * 
		 * @Override public void onSuccess(int statusCode, String content) {
		 * List<Clothes> cloths = JSONObject.parseArray(content, Clothes.class);
		 * 
		 * for (Clothes clths : cloths) { //把季节对应的 id 放入到sp中
		 * SPHelper.putInt(ChooseClothTypeActivity.this,
		 * clths.getSeasonType()+"id", clths.getId()); }
		 * 
		 * // 进行价格排序 Collections.sort(cloths, new Comparator<Clothes>() {
		 * 
		 * @Override public int compare(Clothes c1, Clothes c2) { return
		 * (int)c1.getPrice() - (int)c2.getPrice(); } }); setListView(cloths); }
		 * 
		 * });
		 */
	}

	private void setListView(List<Clothes> cloths) {
		// 首次进入的时候 清除原有sp中的 所选数量
		for (Clothes clths : cloths) {
			SPHelper.clearInt(this, clths.getPrice()+"");
		}

		treeViewAdapter = new ExpandableListViewAdapter(this, ExpandableListViewAdapter.PaddingLeft >> 1, total_cloth_count, total_money);
		expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
		expandableListView.setGroupIndicator(null);
		List<ExpandableListViewAdapter.TreeNode> treeNode = treeViewAdapter.GetTreeNode();

		for (Clothes cloth : cloths) {
			ExpandableListViewAdapter.TreeNode node = new ExpandableListViewAdapter.TreeNode();
			node.parent = cloth.getPrice() + "";
			node.childs = cloth;
			treeNode.add(node);
		}
		treeViewAdapter.UpdateTreeNode(treeNode);
		expandableListView.setAdapter(treeViewAdapter);
		for (int i = 0; i < cloths.size(); i++) {
			expandableListView.expandGroup(i);
		}
	}

	@Override
	public void onBackPressed() {
		Intent intent = new Intent(ChooseClothTypeActivity.this, MainActivity.class);
		startActivity(intent);
		finish();
	}

	@Override
	public void onClick(View view) {

		switch (view.getId()) {
		case R.id.choose_cloth_goback:
			// 跳转到洗衣 fragment
			Intent intent = new Intent(ChooseClothTypeActivity.this, MainActivity.class);
			startActivity(intent);
			finish();
			break;
		case R.id.ensure_choice:
			// 跳转到支付确认页面
			Intent intent2 = new Intent(ChooseClothTypeActivity.this, EnsureChoiceOfClothActivity.class);
			intent2.putExtra("total_count", total_cloth_count.getText().toString());
			intent2.putExtra("total_money", total_money.getText().toString());
			startActivity(intent2);
			break;

		default:
			break;
		}
	}

}
